package com.example.dangerouscargodriver.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dangerouscargodriver.R;

/**
 * Created by Muxd on 2018/12/7.
 */

public class ActionBar extends RelativeLayout {

	private final static int START = 0;
	private final static int CENTER = 1;
	private final static int END = 2;

	private View mBackLayout;
	private View mMenuLayout;
	private ImageView mBackIv;
	private ImageView mMenuIv;
	private ImageView mMenuImage;
	private TextView mTitleTv;
	private Button mMenuBtn;
	private boolean hasMenu;

	public ActionBar(Context context) {
		this(context, null);
	}

	public ActionBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public ActionBar(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context, attrs);
	}

	public void init(Context context, AttributeSet attrs) {
		LayoutInflater.from(context).inflate(R.layout.layout_action_bar, this);

		mBackLayout = findViewById(R.id.layout_back);
		mMenuLayout = findViewById(R.id.layout_menu);
		mBackIv = findViewById(R.id.iv_back);
		mTitleTv = findViewById(R.id.tv_title);
		mMenuImage = findViewById(R.id.tv_menu);
		mMenuIv = findViewById(R.id.iv_menu);
		mMenuBtn = findViewById(R.id.btn_menu);

		TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ActionBar);
		Drawable back = array.getDrawable(R.styleable.ActionBar_backSrc);
		Drawable menu = array.getDrawable(R.styleable.ActionBar_menuSrc);
		Drawable menuImage = array.getDrawable(R.styleable.ActionBar_menuImage);
		String menuIdentify = array.getString(R.styleable.ActionBar_menuIdentify);
		String title = array.getString(R.styleable.ActionBar_title);
		boolean showMenu = array.getBoolean(R.styleable.ActionBar_showMenu, false);
		ColorStateList titleColor = array.getColorStateList(R.styleable.ActionBar_titleColor);
		int titleSize = array.getDimensionPixelSize(R.styleable.ActionBar_titleSize, -1);
		boolean showBack = array.getBoolean(R.styleable.ActionBar_showBack, true);
		int titleGravity = array.getInt(R.styleable.ActionBar_title_gravity, CENTER);
		String menuBtnTitle = array.getString(R.styleable.ActionBar_menuBtn);

		array.recycle();

		if (!showBack) {
			mBackIv.setVisibility(INVISIBLE);
		}

		if (back != null) {
			mBackIv.setVisibility(VISIBLE);
			mBackIv.setImageDrawable(back);
		}

		if (!showMenu) {
			hasMenu = false;
			mMenuIv.setVisibility(INVISIBLE);
			mMenuImage.setVisibility(INVISIBLE);
		}

		if (menuImage != null) {
			hasMenu = true;
			mMenuIv.setVisibility(INVISIBLE);
			mMenuImage.setVisibility(VISIBLE);
			mMenuImage.setImageDrawable(menuImage);
		}

		if (menu != null) {
			hasMenu = true;
			mMenuIv.setVisibility(VISIBLE);
			mMenuImage.setVisibility(INVISIBLE);
			mMenuIv.setImageDrawable(menu);
		}

		if (!TextUtils.isEmpty(title)) {
			mTitleTv.setText(title);
		}

		if (titleColor != null) {
			mTitleTv.setTextColor(titleColor);
		}

		if (titleSize != -1) {
			mTitleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
		}

		if (!TextUtils.isEmpty(menuBtnTitle)) {
			mMenuBtn.setVisibility(VISIBLE);
			mMenuBtn.setText(menuBtnTitle);
		}

		switch (titleGravity) {
			case START:
				mTitleTv.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
				break;
			case CENTER:
				mTitleTv.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);
				break;
			case END:
				mTitleTv.setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
				break;
		}

	}

	public void setOnBackClickListener(OnClickListener listener) {
		mBackLayout.setOnClickListener(listener);
	}

	public void setOnMenuClickListener(OnClickListener listener) {
		if (hasMenu) {
			mMenuLayout.setOnClickListener(listener);
		}
	}

	public void setOnMenuBtnClickListener(OnClickListener listener) {
		mMenuBtn.setOnClickListener(listener);
	}

	public void setTitle(int resId) {
		setTitle(getResources().getString(resId));
	}

	public void setTitle(String title) {
		mTitleTv.setText(title);
	}

	public void setBackVisibility(int visibility) {
		mBackIv.setVisibility(visibility);
	}

	public void setMenuImage(int menuImage) {
		mMenuImage.setImageResource(menuImage);
	}

	public void setMenuEnable(boolean enable) {
		mMenuLayout.setEnabled(enable);
	}
}
